/*
 * VisualTextEntity.java
 *
 * Created on Jan 30, 2008 at 7:08:15 PM
 *
 * Copyright 2009 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.visuals;

import java.awt.Font;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;

/**
 * Specializes the {@link VisualEntity} to allow the creation of an entity that is
 * a piece of text.
 *
 * @author Brandon Franklin
 */
public class VisualTextEntity extends VisualEntity {

    /** The {@link FontRenderContext} used when rendering the text */
    private static final FontRenderContext CONTEXT = new FontRenderContext(new AffineTransform(), true, false);

    /** The {@link Font} used when rendering the text */
    private Font font;

    /** The {@link GlyphVector} derived from the font and text */
    private GlyphVector glyphVector;

    /** The text being displayed */
    private String text;

    /**
     * Creates a new instance of {@link VisualTextEntity} and prepares it for use.
     */
    public VisualTextEntity() {
        font = Font.decode("Dialog-PLAIN-12");
    }

    /**
     * Returns the {@link Font} that is being used to render the text.
     *
     * @return the {@link Font} that is being used to render the text
     */
    public Font getFont() {
        return font;
    }

    /**
     * Returns the text that is being rendered.
     *
     * @return the text that is being rendered
     */
    public String getText() {
        return text;
    }

    /**
     * Sets the {@link Font} that will be used to render the text. If the
     * value provided is null, a suitable default will be applied instead.
     *
     * @param font the {@link Font} to render the text with
     */
    public void setFont(final Font font) {
        if(font == null ) {
            throw new IllegalArgumentException("Font cannot be null.");
        }
        this.font = font;

        if (text != null) {
            glyphVector = this.font.createGlyphVector(CONTEXT, text);
            setShape(glyphVector.getOutline());
        }
    }

    /**
     * Sets the text that will be rendered.
     *
     * @param text the text {@link String} to render
     *
     * @throw {@link IllegalArgumentException} if the provided {@link String} is null
     */
    public void setText(final String text) {
        if(text == null ) {
            throw new IllegalArgumentException("Text cannot be null.");
        }
        this.text = text;

        if (font != null) {
            glyphVector = font.createGlyphVector(CONTEXT, text);
            setShape(glyphVector.getOutline());
        }
    }

}
